<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include '/util/Constant.php';?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Benvenuto in Q?+</title>
</head>

<body>
<?php
$error = "";
$idModulo = "";
if (isset($_GET['tn']))
	$idModulo = $_GET['tn'];
if (isset($_GET['user']))
	$username = $_GET['user'];
if (!$idModulo || $username)
{
	if(isset($_POST['login']) || isset($_GET['login']))
	{
		if(isset($_POST['username']))
			$username = $_POST['username'];
		if(isset($_POST['password']))
			$password = $_POST['password'];
		if(isset($username) && isset($password) && $username!="" && $password!="")
		{
			include 'database/SqlManager.php';
			$sqlManager = new SqlManager();
			$loginOk = $sqlManager->loginCorretta($username, $password);
			//echo $loginOk;
			if($loginOk)
			{
				setcookie(Constant::NOME_COKIE, $username, time()+1800/*30 minuti*/);
				header("Location: amministrazione/menu.php");
			}else{
				$error = "Username o password errati";
			}
		}else{
			$error = "Mancano username e password";
		}
	}
	?>
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<input type="text" id="username" name="username"/>
			<input type="password" id="password" name="password"/>
			<input type="submit" id="login" name="login" value="Login"/>
		</form>
	<?php 
		
	}else{
	//Qui faccio comparire la giusta pagina da visualizzare
}

echo $error;
?>
</body>
</html>